library(tidyverse)
library(ggplot2)
library(dplyr)
library(readxl)
library(RColorBrewer)

options(device = "windows")

Figure_1_Data <- read_excel("C:/Users/evanl/OneDrive/Desktop/Projects/SAI Project/SAI Analysis/Analysis/Graphs/Figure 1 Data.xlsx")

Figure_1_Data$Percentage<-Figure_1_Data$Proportion*100

Figure_1_Data$Percentage<-ifelse(Figure_1_Data$Direction == "Positive", Figure_1_Data$Percentage, -Figure_1_Data$Percentage)

Figure_1_Data$Frame <- factor(Figure_1_Data$Frame, levels = c('Moral Hazard',
                                                              'National Security',
                                                              'Industry Ploy',
                                                              'Risky/Safe',
                                                              'Governance',
                                                              'Economic Costs/Benefits',
                                                              'Equity',
                                                              'Environmental Effects',
                                                              'Efficacy',
                                                              'Unnatural/Natural'))

g1<-ggplot(data=Figure_1_Data, aes(x=Frame, y=Percentage, fill=Direction)) + 
  geom_bar(stat = "identity", position = "identity") +
  scale_y_continuous(limits = c(-100,50), labels = NULL, breaks = NULL) +
  coord_flip() + theme_minimal() +  
  xlab("Emphasis Frame") + ylab("Prominence of Frame in Media Stories") + 
  theme(text=element_text(size=16,  family="serif")) +
  annotate("text", x=10, y=-65, label= "55%",  family="serif") +
  annotate("text", x=9 , y=-52, label= "42%",  family="serif") +
  annotate("text", x=8 , y=-65, label= "55%",  family="serif") +
  annotate("text", x=7 , y=-38, label= "28%",  family="serif") +
  annotate("text", x=6 , y=-37, label= "27%",  family="serif") +
  annotate("text", x=5 , y=-46, label= "36%",  family="serif") +
  annotate("text", x=4 , y=-97, label= "87%",  family="serif") +
  annotate("text", x=3 , y=-30, label= "20%",  family="serif") +
  annotate("text", x=2 , y=-42, label= "32%",  family="serif") +
  annotate("text", x=1 , y=-49, label= "39%",  family="serif") +
  annotate("text", x=10, y=50 , label= "40%",  family="serif") +
  annotate("text", x=9 , y=38 , label= "28%",  family="serif") +
  annotate("text", x=8 , y=35 , label= "25%",  family="serif") +
  annotate("text", x=7 , y=21 , label= "11%",  family="serif") +
  annotate("text", x=6 , y=20 , label= "10%",  family="serif") +
  annotate("text", x=5 , y=20 , label= "10%",  family="serif") +
  annotate("text", x=4 , y=20 , label= "10%",  family="serif") +
  annotate("text", x=3 , y=13 , label= "3%",  family="serif") +
  annotate("text", x=2 , y=11 , label= "1%",  family="serif") + 
  annotate("text", x=1 , y=11 , label= "1%",  family="serif")

g11<-g1 +  guides(fill=guide_legend(title="   Frame\nDirection")) + scale_fill_manual(values=c("#737373", "#BDBDBD")) 
g111<-g11 + labs(caption = "Figure 1. Prominence of Emphasis Frames\nin Media Stories: 2014 - 2022") + theme(plot.caption = element_text(hjust = 0.5,vjust=-1, size=20))
g111

